﻿using MediatR;
using Modules.Catalog.Core.Abstractions;
using Modules.Catalog.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules.Catalog.Core.Commands.Register
{
    public class RegisterBrandCommand:IRequest<int>
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

    }

    internal class BrandRegisterHandler : IRequestHandler<RegisterBrandCommand, int>
    {
        private readonly ICatalogDbContext _catalogDbContext;

        public BrandRegisterHandler(ICatalogDbContext catalogDbContext)
        {
            _catalogDbContext = catalogDbContext;
        }

        public async Task<int> Handle(RegisterBrandCommand request, CancellationToken cancellationToken)
        {
            var brand = new Brand()
            {
                Name = request.Name,
                Description = request.Description
            };
            await _catalogDbContext.Brands.AddAsync(brand);
            await _catalogDbContext.SaveChangesAsync(cancellationToken);
            return brand.Id;
        }
    }
}
